12. 整数转罗马数字
为保证权益,题目请参考 12. 整数转罗马数字(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
using namespace std;
class Solution
{
public:
string intToRoman(int num)
{
if (num >= 900)
{
//M
string tmp = "";
while (num >= 1000)
{
tmp += "M";
num -= 1000;
}
if (num >= 900)
{
tmp += "CM";
num -= 900;
}
return tmp + intToRoman(num);
}
else if (num >= 400)
{
string tmp = "";
if (num >= 500)
{
tmp += "D";
num -= 500;
}
else
{
tmp += "CD";
num -= 400;
}
return tmp + intToRoman(num);
}
else if (num >= 90)
{
string tmp = "";
while (num >= 100)
{
tmp += "C";
num -= 100;
}
if (num >= 90)
{
tmp += "XC";
num -= 90;
}
return tmp + intToRoman(num);
}
else if (num >= 40)
{
string tmp = "";
if (num >= 50)
{
tmp += "L";
num -= 50;
}
else
{
tmp += "XL";
num -= 40;
}
return tmp + intToRoman(num);
}
else if (num >= 9)
{
string tmp = "";
while (num >= 10)
{
tmp += "X";
num -= 10;
}
if (num >= 9)
{
tmp += "IX";
num -= 9;
}
return tmp + intToRoman(num);
}
else if (num >= 4)
{
string tmp = "";
if (num >= 5)
{
tmp += "V";
num -= 5;
}
else
{
tmp += "IV";
num -= 4;
}
return tmp + intToRoman(num);
}
else
{
string tmp = "";
while (num > 0)
{
tmp += "I";
num -= 1;
}
return tmp;
}
return "NULL";
}
};
int main()
{
Solution so;
// cout << so.intToRoman(6) << endl;
for (int i = 10; i >= 0; --i)
{
int n;
cin >> n;
cout << so.intToRoman(n) << endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
解决方案2
CPP
C++
#include <iostream>
#include <string>
using namespace std;
class Solution
{
public:
string intToRoman(int num)
{
string tmp = "";
while (num >= 1000)
{
tmp += "M";
num -= 1000;
}
if (num >= 900)
{
tmp += "CM";
num -= 900;
}
if (num >= 500)
{
tmp += "D";
num -= 500;
}
if (num >= 400)
{
tmp += "CD";
num -= 400;
}
while (num >= 100)
{
tmp += "C";
num -= 100;
}
if (num >= 90)
{
tmp += "XC";
num -= 90;
}
if (num >= 50)
{
tmp += "L";
num -= 50;
}
if (num >= 40)
{
tmp += "XL";
num -= 40;
}
while (num >= 10)
{
tmp += "X";
num -= 10;
}
if (num >= 9)
{
tmp += "IX";
num -= 9;
}
if (num >= 5)
{
tmp += "V";
num -= 5;
}
if (num >= 4)
{
tmp += "IV";
num -= 4;
}
while (num > 0)
{
tmp += "I";
num -= 1;
}
return tmp;
}
};
int main()
{
Solution so;
// cout << so.intToRoman(6) << endl;
for (int i = 10; i >= 0; --i)
{
int n;
cin >> n;
cout << so.intToRoman(n) << endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98